import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.log4j.Logger;
import org.bdware.doaclient.DOAClient;
import org.bdware.doip.core.doipMessage.DoipMessage;
import org.bdware.doip.core.model.digitalObject.DigitalObject;
import org.bdware.doip.core.model.digitalObject.DoType;
import org.bdware.doip.core.utils.GlobalConfigurations;
import org.bdware.irp.exception.IrpClientException;
import org.junit.Before;
import org.junit.Test;

public class CreateMetaInRegistry {

    DOAClient client;
    Logger logger = Logger.getLogger(DOAClientTest.class);
    @Before
    public void init(){
        GlobalConfigurations.IRSAddress = "http://127.0.0.1:10001/";
        client = DOAClient.getDOAClientForTest();
    }

    @Test
    public void create(){
        DigitalObject digitalObject = createMetaDO();
        String repoID = "86.5000.470/doip.PKURegistry";
        try {
            DoipMessage response = client.create(repoID,digitalObject);
            logger.debug("response message: " + new String(response.body.getEncodedData()));
        } catch (IrpClientException irpClientException) {
            irpClientException.printStackTrace();
        }
    }

    DigitalObject createMetaDO(){
        DigitalObject digitalObject = new DigitalObject("86.5000.470/do.hairAirBox", DoType.Metadata);
        JsonObject metadata = new JsonObject();
        metadata.addProperty("doGroupName","测试");
        metadata.addProperty("doGroupDesc","测试分组");
        metadata.addProperty("doName","海尔空气盒子");
        metadata.addProperty("doDesc","燕园大厦813，内含空气质量信息DO");
        digitalObject.addAttribute("metadata",new Gson().toJson(metadata));
        digitalObject.addAttribute("owner","86.5000.470/dou.TEST");
        return digitalObject;
    }
}
